package com.mygame.game_server.cmdHandle;

import com.mygame.game_server.User;
import com.mygame.game_server.caster.Broadcaster;
import com.mygame.game_server.conf.AllConf;
import com.mygame.game_server.manager.UserManager;
import com.mygame.game_server.mq.MqProducer;
import com.mygame.game_server.mq.VictorMsg;
import com.mygame.game_server.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Jim
 * @date 20:28 2022/5/7
 * @description 攻击
 * @email:huwmz04153277@126.com
 */
public class UserAttackCmdHandler implements ICmdHandler<GameMsgProtocol.UserAttkCmd> {
    static private Logger logger = LoggerFactory.getLogger(UserAttackCmdHandler.class);

    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserAttkCmd cmd) {
        if (null == cmd || null == ctx) {
            return;
        }
        Integer attkUserId = (Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();

        logger.info("当前线程 = {}",Thread.currentThread().getName());
        if (null == attkUserId) {
            broadcastAttkResult(attkUserId,-1);
            return;
        }

        // 获取目标用户Id；
        int targetUserId = cmd.getTargetUserId();
        // 获取目标用户
        User targetUser = UserManager.getUserById(targetUserId);

        if (null == targetUser) {
            return;
        }

        final int dmgPoint = 10;


        targetUser.hp =  targetUser.hp - 10;
/*        targetUser.hp.set(targetUser.hp.updateAndGet((h)->{
            return h - 10;
        }));*/

        //攻击结果广播
        broadcastAttkResult(attkUserId, targetUserId);
        //广播见血结果
        broadcastSubstractHpResult(targetUserId, dmgPoint);

        if (targetUser.hp<= 0) {
            //广播死亡结果
            broadcastDieResult(targetUserId);

            VictorMsg newMsg = new VictorMsg();
            newMsg.setLoserId(targetUserId);
            newMsg.setWinnerId(attkUserId);

            MqProducer.sendMsg(AllConf.TOPIC_HEROSTORY_VICTOR,newMsg);

        }

/*        if (targetUser.hp <= 0) {
            //广播死亡结果
            broadcastDieResult(targetUserId);
        }*/

    }

    /**
     * 广播攻击结果
     *
     * @param attkUserId
     * @param targetUserId
     */
    private static void broadcastAttkResult(int attkUserId, int targetUserId) {
        if (attkUserId <= 0) {
            return;
        }

        GameMsgProtocol.UserAttkResult.Builder resultBuilder = GameMsgProtocol.UserAttkResult.newBuilder();
        resultBuilder.setAttkUserId(attkUserId);
        resultBuilder.setTargetUserId(targetUserId);

        GameMsgProtocol.UserAttkResult newResult = resultBuilder.build();

        Broadcaster.broadcaster(newResult);

    }

    /**
     * 广播见血结果
     *
     * @param targetUserId
     * @param substractHp
     */
    private static void broadcastSubstractHpResult(int targetUserId, int substractHp) {
        if (targetUserId <= 0 || substractHp <= 0) {
            return;
        }

        GameMsgProtocol.UserSubtractHpResult.Builder resultBuilder = GameMsgProtocol.UserSubtractHpResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);
        resultBuilder.setSubtractHp(substractHp);
        GameMsgProtocol.UserSubtractHpResult newResult = resultBuilder.build();
        Broadcaster.broadcaster(newResult);


    }

    /**
     * 广播死亡结果
     * @param targetUserId
     */
    private static void broadcastDieResult(int targetUserId) {
        if (targetUserId <= 0) {
            return;
        }

        GameMsgProtocol.UserDieResult.Builder resultBuilder = GameMsgProtocol.UserDieResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);

        GameMsgProtocol.UserDieResult newResult = resultBuilder.build();
        Broadcaster.broadcaster(newResult);
    }
}
